<?php  
class ControllerCommonColumnLeft extends Controller {
	protected function index() {
		$module_data = array();

        if (!$this->isMobileEnabled()) {
            $this->load->model('checkout/extension');
            $results = $this->model_checkout_extension->getExtensions('module');

            foreach ($results as $result) {
                if ($this->config->get($result['key'] . '_status') && ($this->config->get($result['key'] . '_position') == 'left')) {
                    $module_data[] = array(
                        'code'       => $result['key'],
                        'sort_order' => $this->config->get($result['key'] . '_sort_order')
                    );

                    $this->children[] = 'module/' . $result['key'];
                }
            }
            $this->children[] = 'module/newsletter';

            $sort_order = array();

            foreach ($module_data as $key => $value) {
                $sort_order[$key] = $value['sort_order'];
            }

            array_multisort($sort_order, SORT_ASC, $module_data);

            $this->data['modules'] = $module_data;
        }
        else{
            $this->load->model('catalog/category');
            $this->load->model('tool/seo_url');

            $this->children = array();
            $this->data['modules'] = array();

            $this->data['home'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home');
            $this->data['manufacturer'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer');
            $this->data['about_us'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/information&information_id=4');
            $this->data['term_cond'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/forhandlen');
            $this->data['contact'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/contact');


            $this->data['active_menu'] = 1;
            switch($this->request->get['route']){
                case 'product/manufacturer':
                    $this->data['active_menu'] = 2;
                    break;
                case 'information/information':
                    $this->data['active_menu'] = 3;
                    break;
                case 'information/forhandlen':
                    $this->data['active_menu'] = 4;
                    break;
                case 'information/contact':
                    $this->data['active_menu'] = 5;
                    break;
            }

            $this->data['categories'] = $this->getAllCategories(0);
        }

		$this->id = 'column_left';
		$this->data['template'] = $this->config->get('config_template');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/column_left.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/column_left.tpl';
		} else {
			$this->template = 'default/template/common/column_left.tpl';
		}

		$this->render();
	}

    protected function getAllCategories($parent_id) {
        $path = isset($this->request->get['path']) ? explode('_', $this->request->get['path']) : '';
        $active = end($path);
        $results = $this->model_catalog_category->getCategories($parent_id);
        $data = array();

        if ($results) {
            foreach ($results as $result) {
                $children = $this->model_catalog_category->getCategories($result['category_id']);

                $children_data = array();
                if (in_array($result['category_id'], $path)) {
                    foreach ($children as $child) {
                        $sub_children = array();
                        if (in_array($child['category_id'], $path)) {
                            $s_children = $this->model_catalog_category->getCategories($child['category_id']);
                            foreach ($s_children as $s_child) {
                                $link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $result['category_id'] . '_' . $child['category_id'] . '_' . $s_child['category_id'];

                                $sub_children[] = array(
                                    'id' 		=> $s_child['category_id'],
                                    'name'		=> $s_child['name'],
                                    'href'		=> $this->model_tool_seo_url->rewrite($link),
                                    'path'		=> $result['category_id'] . '_' . $child['category_id'] . '_' . $s_child['category_id'],
                                    'children' 	=> null,
                                    'active'    => $s_child['category_id'] == $active,
                                );
                            }
                        }

                        $link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $result['category_id'] . '_' . $child['category_id'];
                        $children_data[] = array(
                            'id' 		=> $child['category_id'],
                            'name'		=> $child['name'],
                            'href'		=> $this->model_tool_seo_url->rewrite($link),
                            'path'		=> $result['category_id'] . '_' . $child['category_id'],
                            'children' 	=> $sub_children,
                            'active'    => $s_child['category_id'] == $active,
                        );
                    }
                }

                if($children || !$path){
                    //$link = HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $result['category_id'];
                    $link = HTTP_SERVER . 'index.php?route=product/manufacturer/category&amp;path=' . $result['category_id'];
                }
                else{
                    $link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $result['category_id'];
                }

                $data[] = array(
                    'id' 		=> $result['category_id'],
                    'name'		=> $result['name'],
                    'href'		=> $this->model_tool_seo_url->rewrite($link),
                    'path'		=> $result['category_id'],
                    'children' 	=> $children_data,
                    'active'    => $s_child['category_id'] == $active,
                );
            }
        }

        return $data;
    }
}
?>